package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.bo.CommonConfigBo;
import com.ruoyi.system.domain.vo.CommonConfigVo;
import com.ruoyi.system.service.ICommonConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 配置信息存储
 *
 * @author ruoyi
 * @date 2023-12-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/commonConfig")
public class CommonConfigController extends BaseController {

    private final ICommonConfigService iCommonConfigService;

    /**
     * 查询配置信息存储列表
     */
    //@SaCheckPermission("system:config:list")
    @GetMapping("/list")
    public TableDataInfo<CommonConfigVo> list(CommonConfigBo bo, PageQuery pageQuery) {
        return iCommonConfigService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出配置信息存储列表
     */
    //@SaCheckPermission("system:config:export")
    @Log(title = "配置信息存储", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CommonConfigBo bo, HttpServletResponse response) {
        List<CommonConfigVo> list = iCommonConfigService.queryList(bo);
        ExcelUtil.exportExcel(list, "配置信息存储", CommonConfigVo.class, response);
    }


    @GetMapping("/noList")
    public R<List<CommonConfigVo>> noList(CommonConfigBo bo) {
        return R.ok(iCommonConfigService.queryList(bo));
    }


    @PostMapping("/saveConfig")
    public R saveConfig(@RequestBody CommonConfigBo bo) {
        iCommonConfigService.saveConfigTypeInfo(bo);
        return R.ok();
    }

    /**
     * 获取配置信息存储详细信息
     *
     * @param id 主键
     */
    //@SaCheckPermission("system:config:query")
    @GetMapping("/{id}")
    public R<CommonConfigVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iCommonConfigService.queryById(id));
    }

    /**
     * 新增配置信息存储
     */
    //@SaCheckPermission("system:config:add")
    @Log(title = "配置信息存储", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CommonConfigBo bo) {
        return toAjax(iCommonConfigService.insertByBo(bo));
    }

    /**
     * 修改配置信息存储
     */
    //@SaCheckPermission("system:config:edit")
    @Log(title = "配置信息存储", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CommonConfigBo bo) {
        return toAjax(iCommonConfigService.updateByBo(bo));
    }

    /**
     * 删除配置信息存储
     *
     * @param ids 主键串
     */
    //@SaCheckPermission("system:config:remove")
    @Log(title = "配置信息存储", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iCommonConfigService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
